package p2p;

import org.mpisws.p2p.filetransfer.FileTransfer;
import org.mpisws.p2p.filetransfer.FileTransferImpl;

import rice.p2p.commonapi.appsocket.AppSocket;
import rice.p2p.commonapi.appsocket.AppSocketReceiver;

public class SyncFileAppSocketReceiver implements AppSocketReceiver 
{
	private SyncFileApp app;
	protected FileTransfer fileTransfer;

	public SyncFileAppSocketReceiver(SyncFileApp app)
	{
		this.app = app;
	}
	/**
     * When we accept a new socket.
     */
    public void receiveSocket(AppSocket socket) 
    {
      fileTransfer = new FileTransferImpl(socket,
    		  new SyncFileTransferCallback(app), app.getNode().getEnvironment());
      
      fileTransfer.addListener(new SyncFileListener());
      // it's critical to call this to be able to accept multiple times
      app.endpoint.accept(this);
    }    

    /**
     * Called when the socket is ready for reading or writing.
     */
    public void receiveSelectResult(AppSocket socket, boolean canRead, boolean canWrite) {
      throw new RuntimeException("Shouldn't be called.");
    }
  
    /**
     * Called if we have a problem.
     */
    public void receiveException(AppSocket socket, Exception e) {
      e.printStackTrace();
    }    
 
}
